package com.wsx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsx.dto.DishDto;
import com.wsx.entity.Dish;
import com.wsx.entity.DishFlavor;
import com.wsx.mapper.DishMapper;
import com.wsx.service.DishFlavorService;
import com.wsx.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        log.info("查看DishDto,{}",dishDto);

        //保存菜品的基本信息到菜品表dish
        dishMapper.insertDishDto(dishDto);
/*        this.save(dishDto);*/

        //菜品id
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream()
                .map(new Function<DishFlavor, Object>() {
                    @Override
                    public Object apply(DishFlavor dishFlavor) {
                        dishFlavor.setDishId(dishId);
                        return dishFlavor;
                    }
                }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.insertBatchDishFlavor(flavors);
/*        dishFlavorService.saveBatch(flavors);*/
    }


    /**
     * 根据id查询菜品信息和对应的口味信息
     */
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        /*Dish dish = this.getById(id);*/
        Dish dish = dishMapper.selectDishById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        /*LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);*/
        Long id1 = dish.getId();
        DishFlavor dishFlavor = new DishFlavor();
        dishFlavor.setDishId(id1);
        List<DishFlavor> flavors = dishFlavorService.selectDishFlavorList(dishFlavor);

        dishDto.setFlavors(flavors);
        return dishDto;
    }


    /**
     * 修改菜品
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        /*this.updateById(dishDto);*/
        dishMapper.updateDishById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        /*LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);*/
        Long id = dishDto.getId();
        DishFlavor dishFlavor = new DishFlavor();
        dishFlavor.setDishId(id);
        dishFlavorService.deleteDishFlavorById(dishFlavor);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream()
                .map(new Function<DishFlavor, Object>() {
                    @Override
                    public Object apply(DishFlavor dishFlavor) {
                        dishFlavor.setDishId(dishDto.getId());
                        return dishFlavor;
                    }
                }).collect(Collectors.toList());

        /*dishFlavorService.saveBatch(flavors);*/
        dishFlavorService.insertBatchDishFlavor(flavors);
    }
}